﻿using Abp;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using AutoMapper.QueryableExtensions;
using FwProject.Product.Dto;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.Product;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;

namespace FwProject.Product
{
    public class ProductTypeAppService : FwProjectAppServiceBase, IProductTypeAppService
    {
        private readonly IRepository<ProductType, Guid> _repositoryProductType;
        public IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }
        public ProductTypeAppService(IRepository<ProductType, Guid> repositoryProductType)
        {
            _repositoryProductType = repositoryProductType;
            _guid = SequentialGuidGenerator.Instance;
        }

        public async Task<LayerDataBaseDto> GetLayerDataAsync(int page, int limit, string keyword)
        {
            var query = _repositoryProductType.GetAll();

            if (!string.IsNullOrWhiteSpace(keyword))
                query = query.Where(o => o.TypeName.Contains(keyword));

            var pagelist = await query.OrderByDescending(o => o.CreationTime).ProjectTo<ProductTypeDto>().ToPagedListAsync(page, limit);

            LayerDataBaseDto dto = new LayerDataBaseDto();
            dto.Data = pagelist;
            dto.Count = pagelist.TotalItemCount;
            return dto;
        }

        public async Task<ProductTypeInput> GetEnterDtoAsync(Guid id)
        {
            var entity =await _repositoryProductType.GetAll().Where(o=>o.Id==id).FirstOrDefaultAsync();

            return entity.MapTo<ProductTypeInput>();
        }


        public async Task<Guid> CreateAsync(ProductTypeInput input)
        {
            var model = _map.Map<ProductType>(input);
            model.Id = _guid.Create();


            var id = await _repositoryProductType.InsertAndGetIdAsync(model);
            return id;
        }

        public async Task<Guid> ModifyAsync(Guid id, ProductTypeInput input)
        {
            var model = await _repositoryProductType.GetAsync(id);
            model = _map.Map(input, model);
            var entity = await _repositoryProductType.UpdateAsync(model);
            return entity.Id;
        }


        public async Task DeleteAsync(Guid id)
        {
            var entity = await _repositoryProductType.GetAsync(id);
            await _repositoryProductType.DeleteAsync(entity);
        }


        public SearchSuggestDto GetSearchSuggestData(string keyword)
        {
            var query = _repositoryProductType.GetAll();

            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(o => o.TypeName.Contains(keyword));

            SearchSuggestDto dto = new SearchSuggestDto();
            dto.Data = query.Select(o => new SearchData { Id = o.Id.ToString(), Word = o.TypeName, Description = o.CreationTime.ToString("yyyy-MM-dd") }).ToList();
            return dto;
        }
    }
}
